<?php
require('includes/application_top.php');
require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_DEFAULT);
if (!tep_session_is_registered('customer_id')) {
    $navigation->set_snapshot(array('mode' => 'SSL', 'page' => FILENAME_CHECKOUT_PAYMENT));
    tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
}
define("WEB_SERVICE" ,'https://ws.onlink.vn/gateway?wsdl');
define("CACHE_PATH", '/tmp/pear/cache/');
define("MY_CAPATH" ,'/usr/local/ssl/certs');
ini_set('soap.wsdl_cache','0');
ini_set('soap.wsdl_cache_enabled','0');
require_once DIR_FS_CATALOG . DIR_WS_CLASSES . 'NuSOAP/nusoap.php';
require(DIR_WS_CLASSES . 'payment.php');
$payment_modules = new payment($payment);

$sql_data_array = array(
		'date' => 'now()'
	);

tep_db_perform(TABLE_TRANSACTION_ORDER, $sql_data_array);
$insert_id = tep_db_insert_id();
$ten = 10;
$i = 1;
while (($inser_id/$ten) != 0) {
	$ten = $ten * 10;
	$i ++;
}
$transactionID = '';
for ($i; $i < 12; $i++) {
	$transactionID .= '0';
}
$transactionID = 'OKKO'.$transactionID.$insert_id;
$orderInfos = array('merchantTransactionID' => $transactionID,
					'deliveryDate' => date('Y/m/d', mktime(0, 0, 0, date('m'), date('d') + 3, date('Y'))),
					'originalAmount' => $HTTP_POST_VARS['onlinkpayment_original_amount'],
					'originalCurrencyShortName' => $HTTP_POST_VARS['onlinkpayment_original_current_short_name'],
					'cardID' => $HTTP_POST_VARS['onlinkpayment_card_issuer'],
					'cardHolderName' => $HTTP_POST_VARS['onlinkpayment_card_holder_name'],
					'cardHolderID' => $HTTP_POST_VARS['onlinkpayment_card_holder_id'],
					'cardHolderCIF' => $HTTP_POST_VARS['onlinkpayment_card_holder_cif'],
					'cardNumber' => $HTTP_POST_VARS['onlinkpayment_card_number'],
					'cardExpireDate' => $HTTP_POST_VARS['onnlinkpayment_expires_month'].'/'.$HTTP_POST_VARS['onlinkpayment_expires_year'],
					'cardAddress' => $HTTP_POST_VARS['onlinkpayment_card_address'],
					'cardCVV' => $HTTP_POST_VARS['onlinkpayment_card_cvv'],
					'billingName' => $HTTP_POST_VARS['onlinkpayment_card_billing_name'],
					'billingCardID' => $HTTP_POST_VARS['onlinkpayment_card_billing_id'],
					'billingAddress' => $HTTP_POST_VARS['onlinkpayment_card_billing_address'],
					'billingPhone' => $HTTP_POST_VARS['onlinkpayment_card_billing_phone'],
					'billingEmail' => $HTTP_POST_VARS['onlinkpayment_card_billing_email'],
					'shippingName' => $HTTP_POST_VARS['onlinkpayment_card_shipping_name'],
					'shippingCardID' => $HTTP_POST_VARS['onlinkpayment_card_shipping_id'],
					'shippingAddress' => $HTTP_POST_VARS['onlinkpayment_card_shipping_address'],
					'shippingPhone' => $HTTP_POST_VARS['onlinkpayment_card_shipping_phone'],
					'shippingEmail' => $HTTP_POST_VARS['onlinkpayment_card_shipping_email'],
					'expireDate' => date('Y/m/d', mktime(0, 0, 0, date('m'), date('d') + 5, date('Y'))),
					'customerIP' => $_SERVER['REMOTE_ADDR'],
					'paymentMethod' => $HTTP_POST_VARS['onlinkpayment_payment_method']);								

$result = $payment_modules->sendOrder($orderInfos);
//var_dump($orderInfos );exit;
if ($result['errorCode'] == 0) {
	if (isset($result['nextURL']) && $result['nextURL'] != '') {
		//tep_redirect(tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
		tep_redirect($result['nextURL']);
	}
	else {
		$param = 'transactionNO='.$result['transactionNo'].'&transaction_order_id='.$insert_id.'&next_url=https://ebanking.dongabank.com.vn';	
		tep_redirect(tep_href_link(FILENAME_CHECKOUT_PROCESS, $param, 'SSL'));
	}
} else {
	tep_db_query("delete from " . TABLE_TRANSACTION_ORDER . " where transaction_id = '" . $insert_id . "'");
}
//exit;
?>

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="stylesheet.css">  
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<?php print_r($result);echo $result['errorMessage']['VIE'];?>
</body>